////////////////////////////////////////////////////////////////////////////////
//
//  FLEX USER GROUP HOKURIKU
//  Copyright 2009 Flex User Group Hokuriku
//  All Rights Reserved.
// 
//  NOTICE: Flex User Group Hokuriku permits you to use, 
//  modify, and distribute this file 
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package net.fxug.hokuriku.chocolat.helper
{
    import flash.events.Event;
    import flash.events.MouseEvent;

    import mx.core.IMXMLObject;
    import mx.events.FlexEvent;

    import net.fxug.hokuriku.chocolat.events.ViewChangeEvent;
    import net.fxug.hokuriku.chocolat.view.ContentsContainer;

    /**
     * アプリケーションコンテンツのヘルパークラス
     *
     * @author shoito
     */
    public class ContentsContainerHelper implements IMXMLObject
    {
        private var view:ContentsContainer;

        public function initialized(document:Object, id:String):void
        {
            this.view = document as ContentsContainer;
            this.view.addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
        }

        private function creationCompleteHandler(event:FlexEvent):void
        {
            this.view.minimize.addEventListener(MouseEvent.CLICK, minimize_clickHandler);
            this.view.maximize.addEventListener(MouseEvent.CLICK, maximize_clickHandler);
            this.view.close.addEventListener(MouseEvent.CLICK, close_clickHandler);
            this.view.speaker.addEventListener(MouseEvent.CLICK, speaker_clickHandler);
            this.view.audience.addEventListener(MouseEvent.CLICK, audience_clickHandler);
            this.view.header.addEventListener(MouseEvent.MOUSE_DOWN, header_mouseDownHandler);
        }

        private function minimize_clickHandler(event:Event):void
        {
            this.view.stage.nativeWindow.minimize();
        }

        private function maximize_clickHandler(event:Event):void
        {
            this.view.stage.nativeWindow.maximize();
        }

        private function close_clickHandler(event:Event):void
        {
            this.view.stage.nativeWindow.close();
        }

        private function backHandler(event:Event):void
        {
            this.view.currentState = "modeSelectState";
        }

        private function speaker_clickHandler(event:Event):void
        {
            this.view.currentState = "speakerState";
            this.view.speakerView.addEventListener(ViewChangeEvent.BACK, backHandler);
            this.view.speakerView.addEventListener(ViewChangeEvent.HIDE, hideHandler);
            this.view.speakerView.addEventListener(ViewChangeEvent.SHOW, showHandler);
        }

        private function audience_clickHandler(event:Event):void
        {
            this.view.currentState = "audienceState";
            this.view.audienceView.addEventListener(ViewChangeEvent.BACK, backHandler);
        }

        private function header_mouseDownHandler(event:Event):void
        {
            this.view.stage.nativeWindow.startMove();
        }

        private function hideHandler(event:Event):void
        {
            this.view.stage.nativeWindow.minimize();
        }

        private function showHandler(event:Event):void
        {
            if (this.view.stage.nativeWindow.closed)
            {
                return;
            }

            this.view.stage.nativeWindow.restore();
            this.view.stage.nativeWindow.orderToFront();
        }
    }
}